
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using Andromeda.Global.Business;
using Andromeda.Global.Data.Db;
using Andromeda.Global.Common;
using ForgeIt.Dal.dbo.Validator;

  
namespace ForgeIt.Dal.dbo.Stub
{
	internal abstract class DBContactStub : dboSchema
	{
		#region private fields

		/// <summary>
		/// (fieldname, datatype, ischangeable(!isidentity), isinsertable(!isidentity), isforeignkey, foreigntable
		/// </summary>

		protected readonly DbField foIdContact = new DbField("IdContact", DbType.Int32, true, true, false, false, false, "");

		protected readonly DbField foIdEntity = new DbField("IdEntity", DbType.Int32, false, false, true, true, true, "Entity");

		protected readonly DbField foContactInfo = new DbField("ContactInfo", DbType.String, false, false, true, true, false, "");

		protected readonly DbField foIdContactType = new DbField("IdContactType", DbType.Int32, false, false, true, true, true, "ContactType");

  
        #region local cache - Foreign Keys

        protected DBEntity localIdEntity;
  

        protected DBContactType localIdContactType;
  

        #endregion local cache - Foreign Keys


        #endregion private fields

        #region constructors

        protected DBContactStub()
		{
		}

		protected DBContactStub(bool ignorethis, string foContactInfo, Int32 foIdEntity, Int32 foIdContactType)
		{

			this.foContactInfo.Value = foContactInfo;

			this.foIdEntity.Value = foIdEntity;

			this.foIdContactType.Value = foIdContactType;

		}
	
		#endregion constructors
    
		#region public properties
		
		public override DbConnector CleanEntity
		{
			get { return new DBContact(); }
		}


		public int IdContact
		{
			get { return ObjectConverter.ToInt32(foIdContact.Value); }
		}

		public int IdEntity
		{
			get { return ObjectConverter.ToInt32(foIdEntity.Value); }
			set { foIdEntity.Value = value; }
		}

		public string ContactInfo
		{
			get { return ObjectConverter.ToString(foContactInfo.Value); }
			set { foContactInfo.Value = value; }
		}

		public int IdContactType
		{
			get { return ObjectConverter.ToInt32(foIdContactType.Value); }
			set { foIdContactType.Value = value; }
		}

  
		public override string TableName
		{
			get { return "Contact"; }
		}



        #region Foreign Keys
      
        /// <summary>
        /// Getter method for Object encapsulated view of foreign key
        /// </summary>
        public virtual DBEntity GetFK_IdEntity()
        {
          return localIdEntity ?? (localIdEntity = DBEntity.GetEntity(IdEntity));
        }
        /// <summary>
        /// Setter method for Object encapsulated view of foreign key
        /// </summary>        
        public virtual void SetFK_IdEntity(DBEntity paramIdEntity)
        {
          localIdEntity = paramIdEntity;
          IdEntity = paramIdEntity.IdEntity;
        }
      
        /// <summary>
        /// Getter method for Object encapsulated view of foreign key
        /// </summary>
        public virtual DBContactType GetFK_IdContactType()
        {
          return localIdContactType ?? (localIdContactType = DBContactType.GetEntity(IdContactType));
        }
        /// <summary>
        /// Setter method for Object encapsulated view of foreign key
        /// </summary>        
        public virtual void SetFK_IdContactType(DBContactType paramIdContactType)
        {
          localIdContactType = paramIdContactType;
          IdContactType = paramIdContactType.IdContactType;
        }
      
        #endregion Foreign Keys


		    protected override ValidationModelBase Validator
		    {
			    get { return new DBContactValidator(); }
		    }
		#endregion public properties
    
		#region enumerator

		/// <summary>
		/// This enumerator contains a list of all the fields of the datasource
		/// </summary>
		public enum FieldNames
		{
			IdContact,
			IdEntity,
			ContactInfo,
			IdContactType,

		}

		#endregion enumerator
    
		#region generic field methods

		/// <summary>
		/// Validates if a given  field value is a DbNull
		/// </summary>
		/// <param name="fieldName">The field to validate</param>
		///<returns>true if is DbNull, false otherwise</returns>
		public bool IsDbNull(FieldNames fieldName)
		{
			switch (fieldName)
			{

				case FieldNames.IdContact: return (foIdContact.Value is DBNull);

				case FieldNames.IdEntity: return (foIdEntity.Value is DBNull);

				case FieldNames.ContactInfo: return (foContactInfo.Value is DBNull);

				case FieldNames.IdContactType: return (foIdContactType.Value is DBNull);

			}

			throw new InvalidDataException();
		}

		/// <summary>
		/// Sets the given field value to DbNull
		/// </summary>
		/// <param name="fieldName">The field to set</param>
		public void SetDbNull(FieldNames fieldName)
		{
			switch (fieldName)
			{

				case FieldNames.IdContact:
					foIdContact.Value = DBNull.Value; break;

				case FieldNames.IdEntity:
					foIdEntity.Value = DBNull.Value; break;

				case FieldNames.ContactInfo:
					foContactInfo.Value = DBNull.Value; break;

				case FieldNames.IdContactType:
					foIdContactType.Value = DBNull.Value; break;

			}
		}
		
		#endregion generic field methods
    
        #region static factories

		/// <summary>
		/// Creates a new DB object that can be added to the database as a new row
		/// </summary>
		/// <returns>The instance of the DB Object</returns>
		public static DBContact NewRow
		{
			get { return new DBContact(); }
		}

		/// <summary>
		/// Returns multiple rows from the Datasource by a given set of WHERE clause fields, only includes fields where value != null
		/// </summary>
		/// <example>List of fields to filter by, null to ignore field (null = all).</example>
		/// <returns>A collection with the filtered results. </returns>
		internal static IEnumerable<DBContact> ListEntity(object foIdContact, object foIdEntity, object foContactInfo, object foIdContactType)
		{
			DBContact helper = new DBContact();
			

			helper.foIdContact.Value = foIdContact;

			helper.foIdEntity.Value = foIdEntity;

			helper.foContactInfo.Value = foContactInfo;

			helper.foIdContactType.Value = foIdContactType;


			return helper.GetEntityInstancesList(helper.Fields.ToArray()).Cast<DBContact>();
        }

		/// <summary>
		/// Returns multiple rows from the Datasource by a given set of WHERE clause fields, only includes fields where value != null
		/// </summary>
		/// <example>List of fields to filter by, null to ignore field (null = all).</example>
		/// <returns>A collection with the filtered results. </returns>
		internal static IEnumerable<DBContact> ListEntity()
		{
			return new DBContact().GetEntityInstancesList(new DbField[0]).Cast<DBContact>();
        }

      		/// <summary>
		/// Returns a single row from the datasource by a given set of RUID strong-typed values
		/// </summary>
		/// <returns>The instance for this key or a new one if no match is found.</returns>
		internal static DBContact GetEntity(int foIdContact)
		{
			DBContact helper = new DBContact();

			helper.foIdContact.Value = foIdContact;

			helper = helper.GetEntityInstance(helper.Ruid) as DBContact;
			
			return helper;
		}


        /// <summary>
        /// Returns multiple rows from the Datasource that referentiate a specific foreign object 
        /// </summary>
        /// <param name="foIdEntity">The <see cref="DBEntity"/> object to filter by.</param>
        /// <returns>A collection with the filtered results.</returns>
        public static  IEnumerable<DBContact> ListEntityIdEntity(DBEntity foIdEntity)
        {
          DBContact helper = new DBContact();

          helper.foIdEntity.Value = foIdEntity.IdEntity;

          return helper.GetEntityInstancesList(helper.foIdEntity).Cast<DBContact>();
        }

        /// <summary>
        /// Returns multiple rows from the Datasource that referentiate a specific foreign object 
        /// </summary>
        /// <param name="foIdContactType">The <see cref="DBContactType"/> object to filter by.</param>
        /// <returns>A collection with the filtered results.</returns>
        public static  IEnumerable<DBContact> ListEntityIdContactType(DBContactType foIdContactType)
        {
          DBContact helper = new DBContact();

          helper.foIdContactType.Value = foIdContactType.IdContactType;

          return helper.GetEntityInstancesList(helper.foIdContactType).Cast<DBContact>();
        }

		#endregion static factories
	}
}